package ar.utils;

import java.util.ArrayList;
import java.util.List;

import android.opengl.GLUtils;
import ar.camera.pojo.GLLocation;
import ar.camera.pojo.GeoLocation;
import ar.globals.Globals;

public class LocationUtils {
	public static GLLocation convertFromGeoLocationToGLLocation(
			GeoLocation geoLocation) {
		GLLocation glLocation = new GLLocation();

		float radiusOfEarth = 6.371f;

		float LAT = (float) ((geoLocation.getLatitude() + 0 - Globals.currentGeoLocation.latitude)
				* Math.PI / 180);
		float LNG = (float) ((geoLocation.getLongitude() + 90 - Globals.currentGeoLocation.longitude)
				* Math.PI / 180);

		glLocation.x = (float) (radiusOfEarth * Math.cos(LAT) * Math.cos(LNG));
		glLocation.y = (float) (radiusOfEarth * Math.cos(LAT) * Math.sin(LNG));
		glLocation.z = (float) (radiusOfEarth * Math.sin(LAT));

		glLocation.textInfo = geoLocation.name;

		// Rotate Z: a = LNG-90
		// x' = x cos a -y sin a
		// y' = x sin a + y cos a
		// float tmpX = glLocation.x;
		// float tmpY = glLocation.y;
		// float a = (float) ((geoLocation.getLongitude() - 90) * Math.PI /
		// 180);
		// glLocation.x = (float) (tmpX * Math.cos(a) - tmpY * Math.sin(a));
		// glLocation.y = (float) (tmpX * Math.sin(a) + tmpY * Math.cos(a));

		//

		return glLocation;
	}

	public static List<GLLocation> convertFromListOfGeoLocationsToListOfGLLocations(
			List<GeoLocation> mListOfGeoLocations, GeoLocation geoCurr,
			float multiple) {
		List<GLLocation> mListOfGLLocations = new ArrayList<GLLocation>();
		GLLocation glCurr = convertFromGeoLocationToGLLocation(geoCurr);
		for (GeoLocation geo : mListOfGeoLocations) {
			GLLocation glTmp = convertFromGeoLocationToGLLocation(geo);

			glTmp.x = (glTmp.x - glCurr.x) * multiple;
			glTmp.z = (glTmp.z - glCurr.z) * multiple;
			glTmp.y = (float) Math.sqrt((glTmp.x - glCurr.x)
					* (glTmp.x - glCurr.x) + (glTmp.z - glCurr.z)
					* (glTmp.z - glCurr.z)) / 1000;
			// glTmp.y = (glTmp.y - glCurr.y) * 1000;
			mListOfGLLocations.add(glTmp);
		}
		return mListOfGLLocations;
	}

	public static float distFrom(float x1, float x2, float z1, float z2) {
		float d = 0;
		d = (float) (Math.pow(x1 - x2, 2) + Math.pow(z1 - z2, 2));
		return (float) Math.sqrt(d);
	}
}
